<?php
$sMsg="";
$Action="?mod=news.listcat.add&method=add";
$Submit="Đồng ý";
$TitleTable="Tạo mới";
if(isset($_GET['pid'])){ $pid = $_GET['pid'];}else{ $pid=0;}
if ($Method=="add")
{
	if(isset($_POST["ltt_ten"])) {$ltt_ten=addslashes($_POST["ltt_ten"]);}
	if(isset($_POST["ltt_thutu"])) $ltt_thutu=$_POST["ltt_thutu"];
	if(isset($_POST["ltt_hienthi"])) $ltt_hienthi=$_POST["ltt_hienthi"];
	if (trim($ltt_ten)!="")
	{
			$sql="Insert into ".$prefix."loaitintuc(ltt_ten,ltt_thutu,ltt_hienthi) values('$ltt_ten',$ltt_thutu,$ltt_hienthi)";
			//echo $sql;
			mysql_query($sql);
			Redirect("Đã thêm mới thành công","?mod=news.listcat");
	} 
}
if($Method=="edit")
{
	$id=$_GET["id"];
	$pid=$_GET["pid"];
	$sql="select * from ".$prefix."loaitintuc where ltt_id=$id";
	$result=mysql_query($sql);
	if(mysql_num_rows($result))
	{
		$rows=mysql_fetch_array($result);
		$ltt_ten=stripslashes($rows["ltt_ten"]);
		$ltt_thutu=$rows["ltt_thutu"];
		$ltt_hienthi=$rows["ltt_hienthi"];
		$Submit="Cập nhập";
		$TitleTable="Sửa thông tin";
		$Action="?mod=news.listcat.add&method=update&id=$id";
	}
}

if ($Method=="update")
{
	$Submit="Tạo Mới";
	$id=$_GET["id"];
	$pid=$_GET['pid'];
	if(isset($_POST["ltt_ten"])) {$ltt_ten=addslashes($_POST["ltt_ten"]);}
	if(isset($_POST["ltt_thutu"])) $ltt_thutu=$_POST["ltt_thutu"];
	if(isset($_POST["ltt_hienthi"])) $ltt_hienthi=$_POST["ltt_hienthi"];
	if (trim($ltt_ten)!="")
	{
			$sql="update ".$prefix."loaitintuc set ltt_ten='$ltt_ten',ltt_thutu=$ltt_thutu,ltt_hienthi=$ltt_hienthi where ltt_id=$id";
			//echo $sql;
			mysql_query($sql);
			Redirect("Cập nhập thành công","?mod=news.listcat");
	} 
}
?>
<script language="javascript1.2">
	function CheckFrm(_form)
	{
	if (_form.ltt_ten.value==""){alert("Bạn phải nhập tên danh mục");_form.ltt_ten.focus();return false;}
	if (_form.ltt_thutu.value==""){alert("Bạn phải nhập thứ tự sắp xếp");_form.ltt_thutu.focus();return false;}
	if (_form.ltt_hienthi.value==""){alert("Bạn phải chọn trạng thái");_form.ltt_hienthi.focus();return false;}
	}
</script>
	<?
	$sql = "select count(*) as total from ".$prefix."loaitintuc".$where."";
	$result = mysql_query($sql) or die("Can't connect database");
	$row = mysql_fetch_array($result);
	?>
<!--Bat dau form tao sua -->
<form name="frmHuyen" id="frmHuyen" action="<?=$Action;?>" method="post" onSubmit="return CheckFrm(this);">
	<table width="100%" border="1" style="border-collapse:collapse " bordercolor="#B2DAEE">
	  <tr class="newsList-content-head">
		<td height="28" colspan="2" align="center"><?=$TitleTable?></td>
	  </tr>		  
	  <tr>
	      <td width="254" align="right"><strong>
		  Tên danh mục :</strong></td>
	      <td width="497">
	      <input type="text" name="ltt_ten" value="<?=$ltt_ten;?>" size="40" class="TextBox"> *	      </td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Thứ tự :</strong></td>
		<td width="497">
		<input type="text" name="ltt_thutu" value="<? if($ltt_thutu=="") { echo $row['total']+1;}else { echo $ltt_thutu;}?>" size="10" class="TextBox">*</td>
	  </tr>	  
	  <tr>
		<td width="254" align="right"><strong>Trạng thái :</strong></td>
		<td width="497">
		<select name="ltt_hienthi" id="ltt_hienthi" style="width:100px">
		<option value="1" <? if($ltt_hienthi == 1){ ?> selected="selected" <? } ?>>Kích hoạt</option>
		<option value="0" <? if($ltt_hienthi == 0){ ?> selected="selected" <? } ?>>Không kích hoạt</option>
		</select>
		</td>
	  </tr>
	  <tr>
		<td colspan="2" align="center">
		<input type="submit" name="Submit" value="<?=$Submit?>" class="Button" onClick="return CheckFrmTl();">&nbsp;&nbsp;&nbsp;
		<input type="reset" name="reset" value="Làm lại" class="Button">&nbsp;&nbsp;&nbsp;
		<input type="button" name="back" value="Quay về" class="Button" onclick="GoBack('?mod=news.listcat')"></td>
	  </tr>
	</table>
</form>
	